﻿using System;
using System.Web;
using System.IO;
using System.Data;
using System.Collections;

public class crmop : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //添加访问权限的逻辑,session和server.avariable["refer"]以及主机名称.防止跨域提交表单
        if (context.Request.ServerVariables["HTTP_Referer"] == null || context.Request.ServerVariables["REMOTE_HOST"] == null || context.Request.ServerVariables["SERVER_NAME"] != context.Request.Url.Host)
        {
            context.Response.Write("非法访问!");
            context.Response.End();
            return;
        }
        string action = context.Request.QueryString["ac"];
        switch (action)
        {
            case "filerename": FileRename(context); break;//重命名文件
            case "test": GetFormParasModel(context); break;
            case "tedst": Test(context); break;
            case "changepx": ChangePx(context); break;//改变排序
            case "renametb": Renametb(context); break;//修改数据表名称.

        }
    }


    private void Renametb(HttpContext context)
    {
        string old = context.Request.Form["oldname"];
        string newname = context.Request.Form["newname"];
        try
        {
            BLL.DataBaseHelper.instance.RenameTableName(old, newname);
            context.Response.Write("ok");
        }
        catch
        {
            context.Response.Write("error");
        }
    }

    private void Test(HttpContext context)
    {
        context.Response.Write("fd");
    }
    private void ChangePx(HttpContext context)
    {
        try
        {
            Hashtable hs = new Hashtable();
            string sid = context.Request.QueryString["sid"];//原排序值
            string spx = context.Request.QueryString["spx"];//原排序值
            string toid = context.Request.QueryString["toid"];
            string topx = context.Request.QueryString["topx"];
            string tbname = context.Request.QueryString["tbname"];
            string keyid = context.Request.QueryString["keyid"];
            hs.Add("px", topx);
            BLL.DataBaseHelper.instance.Update(hs, tbname, keyid+"="+ sid);

            Hashtable hs1 = new Hashtable();
            hs1.Add("px", spx);
            BLL.DataBaseHelper.instance.Update(hs1, tbname, keyid+"=" + toid);
            context.Response.Write("ok");
        }
        catch (Exception ex)
        {
            context.Response.Write(ex.Message);
        }
    }

    private void GetFormParasModel(HttpContext context)
    {
        string pramaid = context.Request.QueryString["id"];
        DataTable dt = BLL.DataBaseHelper.instance.GetModel("FormParas", "*", "ID=" + pramaid);
        if (dt.Rows.Count > 0)
        {
            //string paraid = dt.Rows[0]["paraID"].ToString();
            //string paraname = dt.Rows[0]["para_name"].ToString();
            //string datatype = dt.Rows[0]["datatype"].ToString();
            //string maxlength=dt.Rows[0
            //context.Response.ContentType = "application/json";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            context.Response.Write(Common.JSONHelper.DataTableToJson("myjson", dt));
            context.Response.End();
        }

    }

    private void FileRename(HttpContext context)
    {
        string type = "folder";
        type = context.Request.QueryString["type"];
        string localpath = context.Request.QueryString["localpath"];
        string oldname = context.Request.QueryString["oldname"];
        string newname = context.Request.QueryString["newname"];
        if ("folder".Equals(type))
        {
            Directory.Move(context.Server.MapPath(localpath + "/" + oldname), context.Server.MapPath(localpath + "/" + newname));
        }
        else
        {
            File.Move(context.Server.MapPath(localpath + "/" + oldname), context.Server.MapPath(localpath + "/" + newname));
        }
        context.Response.Write("ok");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}